﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GBT.ParamEntity;
using GBT.SystemEnties;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using NLog;
using PersistenceDataLogic.Core;
using QueryDataLogic.Core;

namespace WebAPI_QiSiMealSystem.Controllers
{
    [Route("gbt/[controller]")]
    [ApiController]
    public class MealController : ControllerBase
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        OperationMeal om = new OperationMeal();
        MealReport mr = new MealReport();
        CheckMeal cm = new CheckMeal();

        // GET: gbt/Meal
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // POST: gbt/Meal/MealReport
        [HttpPost("MealReport")]
        public async Task<IActionResult> GetMealReport(JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    joResult = mr.GetMealReport(value);
                }
                catch (Exception ex)
                {
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // POST: gbt/Meal/MealAllHallReport
        [HttpPost("MealAllHallReport")]
        public async Task<IActionResult> GetMealAllHallReport(JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    joResult = mr.GetMealAllHallReport(value);
                }
                catch (Exception ex)
                {
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // POST: gbt/Meal/GetFamilyIncrementalMeal
        [HttpPost("GetFamilyIncrementalMeal")]
        public async Task<IActionResult> GetFamilyIncrementalMeal(JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    joResult = mr.GetFamilyIncrementalMeal(value);
                }
                catch (Exception ex)
                {
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // POST: gbt/Meal/GetInterimIncrementalMeal
        [HttpPost("GetInterimIncrementalMeal")]
        public async Task<IActionResult> GetInterimIncrementalMeal(JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    joResult = mr.GetInterimIncrementalMeal(value);
                }
                catch (Exception ex)
                {
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // POST: gbt/Meal/GetWorkOvertimeMeal
        [HttpPost("GetWorkOvertimeMeal")]
        public async Task<IActionResult> GetWorkOvertimeMeal(JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    joResult = mr.GetWorkOvertimeMeal(value);
                }
                catch (Exception ex)
                {
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // POST: gbt/Meal/MealReportSelectDate
        [HttpPost("MealReportSelectDate")]
        public async Task<IActionResult> GetMealReportSelectDate(JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    joResult = mr.GetMealReportSelectDate(value);
                }
                catch (Exception ex)
                {
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // POST: gbt/Meal/MyMealReport
        [HttpPost("MyMealReport")]
        public async Task<IActionResult> GetMyMealReport([FromBody] System_ReportMeals _ReportMeals)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                string error = "";
                object result = null;
                try
                {
                    result = mr.GetMyMealReport(_ReportMeals);
                }
                catch (Exception ex)
                {
                    error = ex.Message + Environment.NewLine + ex.StackTrace;
                }
                return Ok(new { root = new { data = result }, error });
            });
            return task;
        }

        // POST: gbt/Meal/MealReportSelectDateAll
        [HttpPost("MealReportSelectDateAll")]
        public async Task<IActionResult> GetMealReportSelectDateAll(JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    joResult = mr.GetMealReportSelectDateAll(value);
                }
                catch (Exception ex)
                {
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // POST: gbt/Meal/MealReportSelectDateAllToWindow
        [HttpPost("MealReportSelectDateAllToWindow")]
        public async Task<IActionResult> GetMealReportSelectDateAllToWindow(JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    joResult = mr.GetMealReportSelectDateAllToWindows(value);
                }
                catch (Exception ex)
                {
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // POST: gbt/Meal/AllMealReportInfo
        [HttpPost("AllMealReportInfo")]
        public async Task<IActionResult> GetAllMealReportInfo(System_MealTime mealTime)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                int totalNumber = 0;
                int totalPage = 0;
                string Error = "";
                object result = null;
                try
                {
                    result = mr.GetAllMealReportInfo(mealTime, ref totalNumber, ref totalPage);
                }
                catch (Exception ex)
                {
                    Error = ex.Message + Environment.NewLine + ex.StackTrace;
                    logger.Error(ex);
                }
                return Ok(new { data = result, totalNumber, totalPage });
            });
            return task;
        }

        // POST: gbt/Meal/MealStatistics
        [HttpPost("MealStatistics")]
        public async Task<IActionResult> GetMealStatistics(JObject hallId)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    joResult = mr.GetMealStatistics(hallId);
                }
                catch (Exception ex)
                {
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // POST: gbt/Meal/MealStatisticsSelectDate
        [HttpPost("MealStatisticsSelectDate")]
        public async Task<IActionResult> GetMealStatisticsSelectDate([FromBody] JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    //JObject joParams = JObject.Parse(value);
                    joResult = mr.GetMealStatisticsSelectDate(value);
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // POST: gbt/Meal/AllMealStatisticsReportInfo
        [HttpPost("AllMealStatisticsReportInfo")]
        public async Task<IActionResult> GetAllMealStatisticsReportInfo(System_MealTime mealTime)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                int totalNumber = 0;
                int totalPage = 0;
                string Error = "";
                object result = null;
                try
                {
                    result = mr.GetAllMealStatisticsReportInfo(mealTime, ref totalNumber, ref totalPage);
                }
                catch (Exception ex)
                {
                    Error = ex.Message + Environment.NewLine + ex.StackTrace;
                    logger.Error(ex);
                }
                return Ok(new { data = result, totalNumber, totalPage });
            });
            return task;
        }

        // POST: gbt/Meal/AuditMeal
        [HttpPost("AuditMeal")]
        public async Task<IActionResult> AuditMeal([FromBody] JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    joResult = om.AuditMeal(value);
                }
                catch (Exception ex)
                {
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // POST: gbt/Meal/AddMeal
        [HttpPost("AddMeal")]
        public async Task<IActionResult> PostAddMeal([FromBody] JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                object result = null;
                string Error = "";
                try
                {
                    //JObject joParams = JObject.Parse(value);
                    result = om.AddMeal(value);
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                    Error = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = result, Error });
            });
            return task;
        }

        // POST: gbt/Meal/ModifyMeal
        [HttpPost("ModifyMeal")]
        public async Task<IActionResult> PostModifyMeal([FromBody] ReportMeals value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                object result = null;
                string Error = "";
                try
                {
                    result = om.ModifyMeal(value);
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                    Error = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = result, Error });
            });
            return task;
        }

        // POST: gbt/Meal/FamilyIncrementalMeal
        [HttpPost("FamilyIncrementalMeal")]
        public async Task<IActionResult> PostFamilyIncrementalMeal([FromBody] JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    joResult = om.AddFamilyIncrementalMeal(value);
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // POST: gbt/Meal/InterimIncrementalMeal
        [HttpPost("InterimIncrementalMeal")]
        public async Task<IActionResult> PostInterimIncrementalMeal([FromBody] JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    joResult = om.AddInterimIncrementalMeal(value);
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // POST: gbt/Meal/WorkOvertimeMeal
        [HttpPost("WorkOvertimeMeal")]
        public async Task<IActionResult> PostWorkOvertimeMeal([FromBody] JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    joResult = om.AddWorkOvertimeMeal(value);
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // POST: gbt/Meal/CheckMeal
        [HttpPost("CheckMeal")]
        public async Task<IActionResult> CheckMeal([FromBody] JObject value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                JObject joResult = new JObject
                {
                    ["Error"] = ""
                };
                try
                {
                    joResult = cm.Check(value);
                }
                catch (Exception ex)
                {
                    joResult["Error"] = ex.Message + "\r\n" + ex.StackTrace;
                }
                return Ok(new { root = joResult });
            });
            return task;
        }

        // PUT: gbt/Meal/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE: gbt/ApiWithActions/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
